﻿using System;
using System.Web.Mvc;
using Portal.Core.Adapters;
using Portal.Logic.Providers.SkillsCatalog;
using Portal.Logic.Services.SkillsCatalog;
using Portal.Presentation.Models;
using Portal.Presentation.Providers;
using Portal.Presentation.Services;

namespace Portal.Presentation.Controllers
{
	[CustomHandleError(View = "Error")]
	public class CVController : ControllerBase
	{
		#region Fields

		private readonly ICurriculumVitaeProvider _provider;
		private readonly IUserAdapter _adapter;
		private readonly ICurriculumVitaeService _service;

		#endregion Fields

		#region .ctor

		/// <summary>
		/// Initializes a new instance of the <see cref="CVController" /> class.
		/// </summary>
		/// <param name="security">The session.</param>
		/// <param name="session">The session.</param>
		/// <param name="provider">The provider.</param>
		/// <param name="adapter">The adapter.</param>
		/// <param name="service">The service.</param>
		/// <exception cref="System.ArgumentNullException">security</exception>
		public CVController(ISecurityProvider security, ISessionService session, ICurriculumVitaeProvider provider, IUserAdapter adapter, ICurriculumVitaeService service)
			: base(security, session)
		{
			if (provider == null)
			{
				throw new ArgumentNullException("provider");
			}
			if (adapter == null)
			{
				throw new ArgumentNullException("adapter");
			}
			if (service == null)
			{
				throw new ArgumentNullException("service");
			}
			_service = service;
			_adapter = adapter;
			_provider = provider;
		}

		#endregion .ctor

		//
		// GET: /CV/
		public ActionResult Preview()
		{
			var model = new CVViewModel();
			var user = SecurityProvider.GetUser(GetLogon());
			if (user != null)
			{
				_adapter.Initialize(user);
				model.DisplayName = _adapter.DisplayName;
			}
			
			var cv = _service.GetPreviewBy(user);
			if (cv != null)
			{
				model.CV = cv;
				model.IsCVUpToDate = _service.IsUpToDate(cv, DateTime.Now);
			}
			return View(model);
		}
	}
}